﻿using DAL.Comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class ProductDAL
    {
        private SqlCommand cmd;//定义SqlCommand对象
        public ProductDAL()
        {
            cmd = new SqlCommand();//SqlCommand对象实例化
            cmd.CommandType = CommandType.StoredProcedure;//设置数据库连接对象为存储过程
        }
        /// <summary>
        /// 获取产品类别下拉内容
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet CategoryList()
        {
            cmd.CommandText = "SelectCategoryList";//定义调用的存储过程
            cmd.Parameters.Clear();//清除参数列表
            DataSet ds = SQLHelper.GetDataSet(cmd);
            return ds;
        }
        /// <summary>
        /// 将数据库读库结果返回到实体中
        /// </summary>
        /// <param name="read">读库结果</param>
        /// <returns>Product业务实体bo</returns>
        private static Product GetValueByDataRead(SqlDataReader read)
        {
            Product bo = new Product();
            bo.ProductID = read["ProductID"].ToString().Trim();
            bo.ProductName = read["ProductName"].ToString().Trim();
            bo.SpellingCode = read["SpellingCode"].ToString().Trim();
            bo.BarCode = read["BarCode"].ToString().Trim();
            bo.Special = read["Special"].ToString().Trim();
            bo.Unit = read["Unit"].ToString().Trim();
            bo.Origin = read["Origin"].ToString().Trim();
            bo.PurchasePrice = decimal.Parse(read["PurchasePrice"].ToString().Trim());
            bo.SalePrice = decimal.Parse(read["SalePrice"].ToString().Trim());
            bo.Quantity = int.Parse(read["Quantity"].ToString().Trim());

            bo.Category = new Category();
            bo.Category.CategoryID = read["CategoryID"].ToString().Trim();
            bo.Category.CategoryName= read["CategoryName"].ToString().Trim();

            return bo;
        }
        /// <summary>
        /// 获取产品列表内容
        /// </summary>
        /// <returns></returns>
        public List<Product> GetList()
        {
            cmd.CommandText = "SelectProductList";//指定存储过程名称
            cmd.Parameters.Clear();//清空存储过程参数列表

            List<Product> list = new List<Product>();//定义泛型集合用于存储数据库返回数据

            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            foreach (var item in read)
            {
                Product c = GetValueByDataRead(read);
                list.Add(c);
            }
            read.Close();//关闭读库
            return list;
        }
        /// <summary>
        /// 新增数据方法实现
        /// </summary>
        /// <param name="bo">Category对象bo</param>
        /// <returns>布尔值</returns>
        public bool Insert(Product bo)
        {
            cmd.CommandText = "InsertProduct";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 数据修改方法
        /// </summary>
        public bool Update(Product bo)
        {
            cmd.CommandText = "UpdateProduct";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 数据删除方法
        /// </summary>
        public bool Delete(Product bo)
        {
            cmd.CommandText = "DeleteProduct";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value = bo.ProductID;
            return ForEdit(cmd);
        }
        /// <summary>
        /// 加载存储过程参数方法实现
        /// </summary>
        /// <param name="bo">Category对象 bo</param>
        private void SetPara(Product bo)
        {
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value          = bo.ProductID;
            cmd.Parameters.Add("@ProductName", SqlDbType.NVarChar, 80).Value       = bo.ProductName;
            cmd.Parameters.Add("@SpellingCode", SqlDbType.NVarChar, 40).Value      = bo.SpellingCode;
            cmd.Parameters.Add("@BarCode", SqlDbType.NVarChar, 14).Value           = bo.BarCode;
            cmd.Parameters.Add("@Special", SqlDbType.NVarChar, 40).Value           = bo.Special;
            cmd.Parameters.Add("@Unit", SqlDbType.NVarChar, 6).Value               = bo.Unit;
            cmd.Parameters.Add("@Origin", SqlDbType.NVarChar, 50).Value            = bo.Origin;
            cmd.Parameters.Add("@CategoryID", SqlDbType.NVarChar, 6).Value         = bo.Category.CategoryID;
            cmd.Parameters.Add("@PurchasePrice", SqlDbType.Decimal).Value          = bo.PurchasePrice;
            cmd.Parameters.Add("@SalePrice", SqlDbType.Decimal).Value              = bo.SalePrice;
            cmd.Parameters.Add("@Quantity", SqlDbType.Int).Value                   = bo.Quantity;
        }
        /// <summary>
        /// 针对数据库修改提出公共实现方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据传入的ID查找对应记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product FindByID(string id)
        {
            cmd.CommandText = "FindProductByID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@ProductID", SqlDbType.NVarChar, 6).Value = id;

            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            Product bo = new Product();
            while (read.Read())
            {
                bo = GetValueByDataRead(read);//调用GetValueByDataRead方法将数据库读取到对象中
            }
            read.Close();//关闭读库
            return bo;
        }
    }
}
